<?php
namespace Admin\Controller;
use Tools\AdminController,    Model\UserModel,
    Think\Model,         Model\RoleModel,
    Model\User_informationModel;

class UserController extends AdminController {
    
    function showlist(){
        $usmd = new UserModel();
          
        //分页
        $total = $usmd -> count();
        $rows  = 10;
        $page  = new \Tools\Page($total, $rows);
        $limit = $page -> getLimit();
        
        //联合查询用户信息
        $Model = new Model();
        $sql   = "select * from `c_user` left outer join c_user_information on c_user.user_id=c_user_information.infor_id "
               . "left outer join c_role on c_user.user_role_id=c_role.role_id limit $limit";
        $list  = $Model -> query($sql);
        
        $fpage = $page -> fpage();
        
        $this -> assign('list',$list);       
        $this -> assign('page',$fpage);       
        $this -> display();
    }
    
    
    function add(){
        $rmd  =  new RoleModel();
        $usmd =  new UserModel();
        $usif =  new User_informationModel();
                
        //所有角色
        $info = $rmd -> select();
        
        //收集注册信息
        if(!empty($_POST)) {
            //验证
            $shu   = $usmd -> create($_POST);
            $infos = $usif -> create($_POST);
            if($shu) {
              if($infos){
                    //注册
                    $shu['user_time'] =  time();
                    $shu['user_pwd']  =  password_hash($shu['user_pwd'], PASSWORD_DEFAULT);
                    $user_id = $usmd  -> add($shu);
                    if($user_id) {
                        $infos['infor_id'] =  $user_id;
                        $infor  =  $usif -> add($infos);
                        if($infor) {
                            $this -> redirect('showlist','',3,'注册成功');
                        }else {
                            $this -> redirect('showlist','',3,'用户部分信息记录失败');
                        }
                    }else {
                        $this -> redirect('add','',3,'注册失败');
                    }
              }else {
                $error = $usif -> getError();
                $this  -> alert($error);        
              }               
            }else{
                $error = $usmd -> getError();
                $this  -> alert($error);               
            }                  
        }
          
        $this -> assign('info',$info);
        $this -> display();
    }
    
    
    function delete($id){
        if(is_array($id)){
             $str = implode(',', $id);
         }else{
             $str = $id;
         } 
        $usmd  = new UserModel();
        $usif  = new User_informationModel();
        $info  = $usmd -> delete($str);
        $infob = $usif -> delete($str);
        if($info && $infob) {
            $this -> redirect('showlist');
        }else {
            $this -> redirect('showlist','',3,'删除失败');
        }
    }
    
    
    function xiugai($id){
        $usmd = new UserModel();
        $usif = new User_informationModel();
        $romd = new RoleModel();
        
        //所有角色
        $allrole = $romd -> select();
        
        //获得要修改用户信息
        $user    = $usmd -> find($id);
        $infor   = $usif -> find($id);
        $role_id = $user['user_role_id'];
        $role    = $romd -> find($role_id);
        
        //修改
        if(!empty($_POST)){
            $shu   = $usmd -> create($_POST);
            $infos = $usif -> create($_POST);
            if($shu) {
                if($infos){
                    $u = $usmd -> save($shu);
                    $i = $usif -> save($infos);
                    if($u || $i){
                        $this -> redirect('showlist');
                    }else {
                        $this -> redirect('showlist');
                    }
                }else {
                    $error = $usif -> getError();
                    $this  -> alert($error);   
                }
            }else {
                $error = $usmd -> getError();
                $this  -> alert($error);   
            }
        }
        
        $this -> assign('user',$user);
        $this -> assign('infor',$infor);
        $this -> assign('role',$role);
        $this -> assign('allrole',$allrole);
        $this -> display();
    }
    
    
    function id_find($user_id){
        $Model = new Model();
        
        //查找user_id为$user_id的用户
        $sql   = "select * from `c_user` left outer join c_user_information on c_user.user_id=c_user_information.infor_id "
               . "left outer join c_role on c_user.user_role_id=c_role.role_id where c_user.user_id='{$user_id}'";
        $list  = $Model -> query($sql);

        $this -> assign('list',$list);
        $this -> display(); 
    }
    
    
    function name_find($user_name){
        $Model = new Model();
        
        $sql   = "select * from `c_user` left outer join c_user_information on c_user.user_id=c_user_information.infor_id "
               . "left outer join c_role on c_user.user_role_id=c_role.role_id where c_user.user_name like '%{$user_name}%'";
        $list  = $Model -> query($sql);
        
        $this -> assign('list',$list);
        $this -> assign('user_name',$user_name);
        $this -> display();   
    }
    
}